from google.appengine.api.users import *
from irhelper import *
from models import *

def is_current_user_author_of(object_class, object_id):
    current_user = get_current_user()
    if not current_user:
        return False
    try:
        ob = object_class.get_by_id(int(object_id))
    except:
        return False
    return ob.author == current_user
    
def is_current_user_permitted(object_class = None, object_id=None):
    return is_current_user_admin() or is_current_user_author_of(object_class, object_id)
    
#wrapped default get_current_user    
def get_local_user():
    user = get_current_user()
    if user:
        q = db.GqlQuery("SELECT * FROM LocalUser WHERE user = :1", user)
        local_user = q.get()
        if not local_user:
            local_user = LocalUser(user=user)
            local_user.local_name = user.nickname()
            local_user.put()
        return local_user
    return None
    
